{% extends "catalog/base_generic.html" %}

{% block content %}
    <div class="card mb-4">
        <div class="card-body">
            <h1 class="card-title">{{ book.title }}</h1>
            <p class="card-text"><strong>作者:</strong> <a href="{{ book.author.get_absolute_url }}">{{ book.author }}</a></p>
            <p class="card-text"><strong>简介:</strong> {{ book.summary }}</p>
            <p class="card-text"><strong>ISBN:</strong> {{ book.isbn }}</p>
            <p class="card-text"><strong>类别:</strong> {{ book.display_genre }}</p>

            <!-- 管理员操作按钮 -->
            {% if perms.catalog.can_edit_books %}
                <div class="mt-3">
                    <a href="{% url 'book-update' book.pk %}" class="btn btn-warning btn-sm">编辑书籍</a>
                    <a href="{% url 'book-delete' book.pk %}" class="btn btn-danger btn-sm">删除书籍</a>
                </div>
            {% endif %}
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h3>可用副本</h3>
        </div>
        <div class="card-body">
            {% if book.bookinstance_set.all %}
                <ul class="list-group">
                    {% for copy in book.bookinstance_set.all %}
                        <li class="list-group-item {% if copy.status == 'a' %}list-group-item-success{% elif copy.status == 'o' %}list-group-item-warning{% else %}list-group-item-secondary{% endif %}">
                            <p><strong>副本ID:</strong> {{ copy.id }}</p>
                            <p><strong>状态:</strong> {{ copy.get_status_display }}</p>
                            {% if copy.due_back %}
                                <p><strong>应归还日期:</strong> {{ copy.due_back }}</p>
                            {% endif %}
                            {% if copy.borrower %}
                                <p><strong>借阅者:</strong> {{ copy.borrower }}</p>
                            {% endif %}

                            <!-- 借阅/续借按钮 -->
                            {% if user.is_authenticated %}
                                {% if copy.status == 'a' %}
                                     <a href="{% url 'borrow-book' copy.pk %}" class="btn btn-primary btn-sm">借阅此书</a>
                                {% elif copy.status == 'o' and copy.borrower == user %}
                                     <a href="{% url 'renew-book' copy.pk %}" class="btn btn-info btn-sm">续借此书</a>
                                {% endif %}
                            {% endif %}

                            <!-- 管理员标记归还按钮 -->
                            {% if perms.catalog.can_mark_returned %}
                                <a href="{% url 'return-book' copy.pk %}" class="btn btn-secondary btn-sm">标记为已归还</a>
                            {% endif %}
                        </li>
                    {% endfor %}
                </ul>
            {% else %}
                <p class="text-muted">暂无可用副本</p>
            {% endif %}
        </div>
    </div>
{% endblock %}